package com.goods.business.service.imp;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.goods.business.converter.ConsumerConverter;
import com.goods.business.converter.SupplierConverter;
import com.goods.business.mapper.ConsumerMapper;
import com.goods.business.service.ConsumerService;
import com.goods.common.model.business.Consumer;
import com.goods.common.model.business.Supplier;
import com.goods.common.vo.business.ConsumerVO;
import com.goods.common.vo.business.SupplierVO;
import com.goods.common.vo.system.PageVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author: 赵勇镔
 * @Time: 2021/6/10 14:42
 * @Description:
 */
@Service
public class ConsumerServiceImpl implements ConsumerService {

    @Autowired(required = false)
    private ConsumerMapper consumerMapper;

    //去处信息展示
    @Override
    public PageVO<ConsumerVO> findConsumerList(Integer pageNum, Integer pageSize, ConsumerVO consumerVO) {
        //开启分页
        PageHelper.startPage(pageNum, pageSize);

        //从数据库中查询
        Example o = new Example(Consumer.class);
        //具体地点
        String name = consumerVO.getName();
        //联系人
        String contact = consumerVO.getContact();
        //省市区县
        String address = consumerVO.getAddress();
        Example.Criteria criteria = o.createCriteria();
        if (name != null && !"".equals(name)) {
            criteria.andLike("name", "%" + name + "%");
        }
        if (contact != null && !"".equals(contact)) {
            criteria.andLike("contact", "%" + contact + "%");
        }
        if (address != null && !"".equals(address)) {
            criteria.andLike("address", "%" + address + "%");
        }
        List<Consumer> consumerList = consumerMapper.selectByExample(o);

        //转化为需要的类型
        List<ConsumerVO> consumerVOList = new ArrayList<>();
        //遍历转化为封装类
        for (Consumer consumer : consumerList) {

            //使用封装的方法转化
            ConsumerVO consumerVO1 = ConsumerConverter.converterConsumerVO(consumer);
            //添加到集合
            consumerVOList.add(consumerVO1);
        }

        //转化为分页对象返回
        PageInfo<Consumer> Info = new PageInfo<>(consumerList);
        //前端返回需要的对象
        return new PageVO<>(Info.getTotal(), consumerVOList);

    }

    //添加来源信息
    @Override
    public void add(ConsumerVO consumerVO) {
        //设置创建修改时间
        consumerVO.setCreateTime(new Date());
        consumerVO.setModifiedTime(new Date());
        consumerMapper.insert(ConsumerConverter.converterConsumer(consumerVO));
    }

    //物资来源修改数据回显
    @Override
    public ConsumerVO edit(Long id) {
        Example o = new Example(Consumer.class);
        o.createCriteria().andEqualTo("id", id);
        Consumer consumer = consumerMapper.selectOneByExample(o);

        return ConsumerConverter.converterConsumerVO(consumer);

    }

    //物资来源修改数据保存
    @Override
    public void update(Long id, ConsumerVO consumerVO) {
        //先转化类型
        Consumer consumer = ConsumerConverter.converterConsumer(consumerVO);
        consumer.setModifiedTime(new Date());

        //根据id保存修改内容
        consumerMapper.updateByPrimaryKey(consumer);
    }

    //物资来源修改数据删除
    @Override
    public void delete(Long id) {
        //根据id保存修改内容
        consumerMapper.deleteByPrimaryKey(id);
    }


}
